import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class InputFileParser {

	public static void main(String[] args) {
		
		//String line = "M_Ann_ACH|P|M|123456789|12345678|U12345678||ScanHQ|10/16/2006|1234567|00426987.tif";
		//String line = "O_Agree_Labor|P|O|||11/11/2005|PA1234|ccharron|04/04/2005||00123225.tif";
		String line = "O_Agree_Labor|||||6|7|8|9|10|00123225.tif";
		//String line = "|2||4|5|";
		String delimiter= "|";
		InputRecord inputAttributes = extractAttributesFromDelimitedInput(line, delimiter);
		System.out.println(inputAttributes);
		
	}
	
	public static InputRecord extractAttributesFromDelimitedInput(String line, String delimiter) {
		
		line = delimiter + line + delimiter; // not to miss first and last tokens if they are emtpy
		
		StringTokenizer st = new StringTokenizer(line, delimiter, true);
		List attributeList = new ArrayList();
		
		String prevToken = "";
		int attribCount = 0;
		
		while (st.hasMoreTokens()) {
			
			String token = st.nextToken();
			boolean tokenModified = false;
			if (token.equals(prevToken) && token.equals(delimiter)) {
				token = "";
				tokenModified = true;
			}
			
			if (!token.equals(delimiter)) {
				attributeList.add(token);
				attribCount++;

				System.out.println("attrib #" + attribCount + ":" + token);

				if (tokenModified)
					token = delimiter;
				
			}
			
			prevToken = token;
		}		
		System.out.println("found " + attribCount + " attributes.");
		System.out.println(attributeList);		
		
		if (attribCount != 11) {
			throw new RuntimeException("11 attributes are expected but the input line only has " + attribCount);
		}
		
		// 1: document image type
		// 2: owner class
		// 3: v3 entity
		// 4: v3 index
		// 5: search attribute #2
		// 6: search attribute #1
		// 7: search attribute #3
		// 8: search attribute #4
		// 9: search attribute #5
		// 10: v3 internal id
		// 11: image file name
		
		InputRecord attribs = new InputRecord();		
		int count = 0;
		
		for (Iterator iter = attributeList.iterator(); iter.hasNext(); ) {
			count++;
			String value = (String) iter.next();
			
			switch (count) {
			case 1: attribs.setImageType(value); 	break;
			case 2: attribs.setOwnerClass(value);	break;
			case 3: attribs.setV3Entity(value);		break;
			case 4: attribs.setV3Index(value);      break;
			case 5: attribs.setSearchAttribute2(value); break;
			case 6: attribs.setSearchAttribute1(value); break;
			case 7: attribs.setSearchAttribute3(value); break;
			case 8: attribs.setSearchAttribute4(value); break;
			case 9: attribs.setSearchAttribute5(value); break;
			case 10: attribs.setV3InternalId(value); break;
			case 11: attribs.setImageFilename(value); break;
			default: break;
			}		
		}
		return attribs;		
	}
}

class InputRecord {

	String imageType;
	String ownerClass;
	String v3Entity;
	String v3Index;
	String searchAttribute1;
	String searchAttribute2;
	String searchAttribute3;
	String searchAttribute4;
	String searchAttribute5;
	String v3InternalId;
	String imageFilename;
	
	public InputRecord() {
	}
	
	public String getImageFilename() {
		return imageFilename;
	}
	public void setImageFilename(String imageFilename) {
		this.imageFilename = imageFilename;
	}
	public String getImageType() {
		return imageType;
	}
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}
	public String getOwnerClass() {
		return ownerClass;
	}
	public void setOwnerClass(String ownerClass) {
		this.ownerClass = ownerClass;
	}
	public String getSearchAttribute1() {
		return searchAttribute1;
	}
	public void setSearchAttribute1(String searchAttribute1) {
		this.searchAttribute1 = searchAttribute1;
	}
	public String getSearchAttribute2() {
		return searchAttribute2;
	}
	public void setSearchAttribute2(String searchAttribute2) {
		this.searchAttribute2 = searchAttribute2;
	}
	public String getSearchAttribute3() {
		return searchAttribute3;
	}
	public void setSearchAttribute3(String searchAttribute3) {
		this.searchAttribute3 = searchAttribute3;
	}
	public String getSearchAttribute4() {
		return searchAttribute4;
	}
	public void setSearchAttribute4(String searchAttribute4) {
		this.searchAttribute4 = searchAttribute4;
	}
	public String getSearchAttribute5() {
		return searchAttribute5;
	}
	public void setSearchAttribute5(String searchAttribute5) {
		this.searchAttribute5 = searchAttribute5;
	}
	public String getV3Entity() {
		return v3Entity;
	}
	public void setV3Entity(String entity) {
		v3Entity = entity;
	}
	public String getV3Index() {
		return v3Index;
	}
	public void setV3Index(String index) {
		v3Index = index;
	}
	public String getV3InternalId() {
		return v3InternalId;
	}
	public void setV3InternalId(String internalId) {
		v3InternalId = internalId;
	}
	
	//String scanLocationPath;
	
	public String toString() {
		
		StringBuffer buf = new StringBuffer();
		buf.append("\n1 - image type: " + getImageType());
		buf.append("\n2 - owner class: " + getOwnerClass());
		buf.append("\n3 - v3 entity: " + getV3Entity());
		buf.append("\n4 - v3 index: " + getV3Index());
		buf.append("\n5 - search attribute #2: " + getSearchAttribute2());
		buf.append("\n6 - search attribute #1: " + getSearchAttribute1());
		buf.append("\n7 - search attribute #3: " + getSearchAttribute3());
		buf.append("\n8 - search attribute #4: " + getSearchAttribute4());
		buf.append("\n9 - search attribute #5: " + getSearchAttribute5());
		buf.append("\n10 - v3 internal id: " + getV3InternalId());
		buf.append("\n11 - image file name: " + getImageFilename());
		return getClass().getName() + buf.toString();
	}
	
}
